package com.line.myprojectservice.Interceptor;

import cn.hutool.jwt.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.line.myprojectservice.constant.Constants.JWT_SECRET;

/**
 * @author ：Line
 * @date ： 2022/3/31 15:25
 * @description :TODO
 */
@Component
@Slf4j
public class JwtInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String token = request.getHeader("token");
        try {
            // 1.验证token
            JWTUtil.verify(token, JWT_SECRET);
            // 2.取出JWT字符串载荷中的随机token，从Redis中获取用户信息
            return true;
        } catch (Exception e) {

            log.info("token不正确");
        }
        return false;
    }

}
